<template>
  <view class="member" @touchmove.stop.prevent="e=>{}">
    <u-sticky>
      <view class="tab">
        <u-tabs :list="tabList" @click="tabClick" lineColor="#75CECC" itemStyle="width:33%;height:45px;padding:0" :activeStyle="{color:'#75CECC'}">
        </u-tabs>
      </view>
    </u-sticky>
    <view class="vip">
      <view class="vip-content">
        <view v-for="item in cardList" :key="item.id">
          <view class="swiper-item" :style="{ 'background-image': 'url('+item.url+')' }">
            <view class="text">数量：{{ item.count }} 张</view>
            <view class="top">
              <view class="top-img">
                <view>
                  <image :src="item.avatarUrl" class="avatar-img" />
                </view>
              </view>
              <view class="top-info">
                <view class="name">{{ item.cardName }} </view>
                <view class="date">{{ item.effectiveTime }}<text>{{$t('member.daoQi')}}</text></view>
              </view>
            </view>
            <view class="swiper-bottom">
              <view class="left" @click="getCodeImg(item)">{{$t('member.shengChengEwM')}}</view>
              <view class="right" @click="openPopup">{{$t('member.quGouM')}} ></view>
            </view>
          </view>
        </view>
        <view class="no-data" v-if="!cardList.length">
          <u-empty mode="data" :icon="`${imgPath}/images/index/no_order.png`" width="100px" height="100px" :text="$t('index.zanWuSj')" textColo="#75CECC" />
        </view>
      </view>
    </view>

    <u-popup :show="showPopup" closeable @close="showPopup=false">
      <view class="swiper-content popup-swiper">
        <view class="vip-title">
          <view>
            <image :src="vipTitleIcon" class="vip-icon" />
          </view>
          <view class="text">{{$t('member.kaiTongHuiY')}}</view>
          <view>
            <image :src="vipTitleIcon" class="vip-icon" />
          </view>
        </view>
        <swiper class="swiper swiper-img" :duration="300" @change="popupSwiperChange" :nextMargin="nextMarginSet(swiperCurrent)" :previous-margin="previousMarginSet(swiperCurrent)">
          <swiper-item v-for="item in vipList" :key="item.id">
            <view class="swiper-item" :style="{ 'background-image': 'url('+item.url+')' }">
              <view class="item-top">{{ item.name }}</view>
              <view class="item-middle">￥{{ item.priceStr }}</view>
              <view class="item-bottom">{{$t('member.youXiaoQ')}}:{{ item.validityPeriod }}天</view>
            </view>
          </swiper-item>
        </swiper>
      </view>
      <view class="choice-num">
        <view>{{$t('member.xuanZeSl')}}</view>
        <view><u-number-box v-model="popupCount" @change="countChange" button-size="24" disabledInput /></view>
      </view>
      <view class="price-button" @click="sureBuy">￥{{(vipList[swiperCurrent].priceStr * popupCount|| 0).toFixed(2)}}{{$t('member.liJiGm')}}</view>
    </u-popup>

    <u-popup :show="showCodePopup" closeable @close="showCodePopup=false" round="25">
      <view class="code-popup" :style="{ 'background-image': 'url('+popupUrl+')','height': screenHeight + 'px'}">
        <!-- <image :src="popupUrl" class="popup-bg" /> -->
        <view class="code-img" :style="{ bottom: safeBottom }">
          <image :src="codeImg" class="img" />
        </view>
      </view>
    </u-popup>
  </view>
</template>

<script>
export default {
  computed: {
    imgPath() {
      return this.$config.baseURL + "/static/front";
    },
  },
  watch: {
    vipSwiperCount(val) {
      if (val === 0) {
        this.textCount = 6;
        this.typeText = this.$t("member.baiYin");
      } else if (val === 1) {
        this.textCount = 7;
        this.typeText = this.$t("member.huangJin");
      } else if (val === 2) {
        this.textCount = 8;
        this.typeText = this.$t("member.zuanShi");
      }
    },
  },
  data() {
    return {
      tabList: [
        {
          name: this.$t("member.baiYinHy"),
          value: "1",
        },
        {
          name: this.$t("member.huangJinHy"),
          value: "2",
        },
        {
          name: this.$t("member.zuanShiHy"),
          value: "3",
        },
      ],
      level: "1",
      avatarUrl: "",
      userName: "",
      isAgree: "1",
      vipList: [],
      cardList: [],
      vipCount: 0,
      totalCount: 0,
      showPopup: false,
      popupCount: 1,
      vipSwiperCount: 0,
      swiperCurrent: 0,
      textCount: 6,
      typeText: this.$t("member.baiYin"),
      pageNum: 1,
      pageSize: 10,
      showCodePopup: false,
      popupUrl: "",
      codeImg: "",
      safeBottom: "",
      vipTitleIcon: "",
      screenHeight: "100%",
    };
  },
  onLoad() {
    this.vipTitleIcon = this.imgPath + "/images/icon/member_vip_title.png";
    const info = uni.getSystemInfoSync();
    this.screenHeight = info.windowHeight * 0.98;
    // this.safeBottom = info.safeAreaInsets.bottom;
    // this.safeBottom = `calc(12% + ${info.safeAreaInsets.bottom}px)`;
    this.getVipList();
    this.getAllList();
  },
  onReady() {
    uni.setNavigationBarTitle({
      title: this.$t("page.woDeHyK"),
    });
  },
  onReachBottom() {
    //上拉到底时触发，onReachBottom函数跟生命周期同级
    if (this.cardList.length < this.totalCount) {
      this.pageNum++; //当前条数小于总条数 则增加请求页数
      this.getAllList(); //调用加载数据方法
    } else {
      uni.$u.toast(this.$t("index.yiJiaZaiQb"));
    }
  },
  methods: {
    tabClick(item) {
      this.cardList = [];
      this.pageNum = 1;
      this.level = item.value;
      this.getAllList();
    },
    nextMarginSet(count) {
      if (count === 2) {
        return 0;
      } else {
        return 20;
      }
    },
    previousMarginSet(count) {
      if (count === 2) {
        return 20;
      } else {
        return 0;
      }
    },
    moveHandle() {
      return false;
    },

    async getAllList() {
      const params = {
        level: this.level,
        pageNum: this.pageNum,
        pageSize: this.pageSize,
      };
      const res = await this.$api.myCardlistNew(params);
      if (res.code === 0) {
        this.cardList = [...res.data.list, ...this.cardList];
      } else {
        this.cardList = [];
      }
    },
    async getVipList() {
      const params = {
        pageNum: 1,
        pageSize: 50,
      };
      const res = await this.$api.vipcardList(params);
      if (res.code === 0) {
        this.vipList = res.data.list;
      } else {
        this.vipList = [];
      }
    },
    async getCodeImg(item) {
      const res = await this.$api.getZxCode({ id: item.id });
      if (res.code === 0) {
        this.showCodePopup = true;
        this.popupUrl = res.data.url;
        this.codeImg = "data:image/png;base64," + res.data.base64Str;
      }
    },
    openPopup() {
      this.showPopup = true;
    },
    countChange(e) {
      this.popupCount = e.value;
    },
    popupSwiperChange(e) {
      this.swiperCurrent = e.detail.current;
    },
    async sureBuy() {
      const params = {
        cardId: this.vipList[this.swiperCurrent].id,
        count: this.popupCount,
      };
      const res = await this.$api.vipGenerateOrder(params);
      if (res.code === 0) {
        const payRes = await this.$api.prePay({ orderNumber: res.data });
        if (payRes.code === 0) {
          const that = this;
          const data = JSON.parse(payRes.data.pay_info);
          uni.requestPayment({
            timeStamp: data.timeStamp, // 时间戳
            nonceStr: data.nonceStr, //随机字符串
            // package: payRes.data.prepay_id, //接口返回的prepay_id
            package: data.package,
            signType: data.signType, //签名算法，需要与后台下单时一致
            paySign: data.paySign,
            success: function (resSuccess) {
              if (resSuccess.errMsg == "requestPayment:ok") {
                uni.$u.toast(that.$t("member.gouMaiCg"));
                that.getAllList();
                that.showPopup = false;
              }
            },
            fail: function (err) {
              console.log(err);
              if (err.errMsg == "requestPayment:fail cancel") {
                uni.$u.toast(that.$t("pay.zhiFuQx"));
              } else {
                uni.$u.toast(err.errMsg);
              }
            },
          });
        }
      } else {
        uni.$u.toast(res.msg);
      }
    },
  },
};
</script>

<style lans="scss">
page {
  background-color: #f6f6f6 !important;
}
</style>
<style lang="scss" scoped>
/deep/.u-number-box__minus {
  background-color: transparent !important;
  border: 1px solid #f7bf55;
  border-radius: 12px 12px !important;
  width: 24px !important;
  height: 24px !important;
}
/deep/ .u-number-box__plus {
  background-color: transparent !important;
  border: 1px solid #f7bf55;
  border-radius: 12px 12px !important;
  width: 24px !important;
  height: 24px !important;
}
/deep/ .u-number-box__input {
  background-color: transparent !important;
}

.swiper-item {
  width: 100%;
  height: 170px;
  background-repeat: no-repeat;
  background-size: 100% 100%;
  .text {
    padding: 15px 0 0px 30px;
    color: #fff;
    font-size: 14px;
  }
  .item-top {
    padding: 30px 0 0 30px;
    color: #fff;
  }
  .item-middle {
    height: 50px;
    font-size: 24px;
    padding: 20px 0 0 30px;
    color: #fff;
  }
  .item-bottom {
    text-align: right;
    padding-right: 30px;
    color: #fff;
  }
  .swiper-bottom {
    display: flex;
    height: 40px;
    align-items: center;
    justify-content: space-between;
    font-size: 14px;
    color: #fff;
    padding: 0 30px;
    .right {
      background-color: #fff;
      color: #75bbf1;
      padding: 5px 15px;
      border-radius: 12px;
    }
  }
}
.swiper-content {
  height: 225px;
  width: calc(100% - 20px);
  position: absolute;
  left: 10px;
  top: 35px;
  z-index: 1;
  border-radius: 8px;
  overflow: hidden;

  .swiper-img {
    height: 180px;
  }
}
.price-button {
  height: 45px;
  width: 70%;
  background: linear-gradient(180deg, #0b0b0b 0%, #5c5c5c 100%);
  border-radius: 25px;
  margin: 20px auto 0;
  text-align: center;
  color: #fff;
  line-height: 45px;
  margin-bottom: 30px;
}

.member {
  width: 100%;
  .tab {
    height: 50px;
    background-color: #fff;
  }
  .vip {
    width: 100%;
    overflow: hidden;
    margin-top: 20px;
    .vip-img {
      width: 100%;
      height: 130px;
      position: absolute;
      left: 0;
      top: 0;
      z-index: 0;
    }
    .vip-content {
      width: calc(100% - 20px);
      overflow: hidden;
      margin: 0 auto;
      .top {
        height: 65px;
        overflow: hidden;
        display: flex;
        padding: 10px 25px 0;
        align-items: center;
        .top-img {
          width: 48px;
          height: 48px;
          border-radius: 50%;
          overflow: hidden;
          .avatar-img {
            width: 48px;
            height: 48px;
            border-radius: 50% 50%;
          }
        }
        .top-info {
          flex: 1;
          padding-left: 10px;
          font-size: 14px;
        }
        .name {
          font-size: 14px;
          margin-top: 5px;
          color: #fff;
        }
        .date {
          color: #fff;
          font-size: 14px;
          padding-top: 5px;
          text {
            padding-left: 5px;
          }
        }
        .text {
          color: #fff;
          font-size: 14px;
          margin-left: 10px;
          margin-top: 5px;
        }
        .more {
          font-size: 14px;
          color: #fff;
        }
      }
    }
  }
  .vip-title {
    height: 60px;
    display: flex;
    width: 100%;
    justify-content: center;
    align-items: center;
    .vip-icon {
      width: 26px;
      height: 16px;
    }
    .text {
      padding: 0 15px;
      font-size: 18px;
      color: #333;
    }
  }
  .swiper-wrap {
    position: relative;
    width: 100%;
    height: 280px;
    overflow: hidden;
    position: relative;
    top: -50px;
    .white-bg {
      position: absolute;
      left: 0;
      top: 0;
      width: 100%;
      overflow: hidden;
      height: 280px;
      z-index: 0;
      .white-img {
        height: 280px;
        width: 100%;
      }
    }
  }
}
.popup-swiper {
  position: static;
  padding-bottom: 10px;
  margin: 40px auto 0;
}
.choice-num {
  height: 60px;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 0 20px;
  padding: 0 20px;
  border: 1px solid #333;
  border-radius: 8px;
}
.code-popup {
  position: relative;
  max-height: 614px;
  background-size: 100% 100%;
  background-repeat: no-repeat;
  background-position: top left;
  border-radius: 20px;

  .popup-bg {
    width: 100%;
    height: 100%;
  }
  .code-img {
    width: 80px;
    height: 80px;
    position: absolute;
    z-index: 10;
    left: 10%;
    bottom: 12%;
    .img {
      width: 80px;
      height: 80px;
    }
  }
}
// /deep/.u-slide-up-enter-active {
//   max-height: 614px;
//   border-radius: 20px;
//   overflow: hidden;
// }
</style>
